package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     */
    @GetMapping("/list")
    public PageDTO<Item> Page(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        Page<Item> page1 = itemService.page(new Page<>(page, size));
        return new PageDTO<>(page1.getTotal(), page1.getRecords());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item itemById(@PathVariable("id") Long id){
        return itemService.getById(id);
    }

    /**
     * 新增
     * @param item
     */
    @PostMapping
    public void itemsave(@RequestBody Item item){
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(1);
//        新增物品
        itemService.save(item);
    }

    /**
     * 上下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void itemstatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        itemService.updateStatus(id,status);
    }

    /**
     * 修改
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item){
        item.setUpdateTime(new Date());
        item.setStatus(null);
        itemService.updateById(item);
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("{id}")
    public void DeleteById(@PathVariable("id") Long id){
        itemService.removeById(id);
    }
}

